package com.shiguiwu.springmybatis.spring.transaction.propagation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description:
 * @author: stone
 * @date: Created by 2021/6/15 9:50
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.transaction.propagation
 */
@Service
public class TxService2 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void required(String bookName) {
        //http://124.70.98.107:8090/riskcensus/app/onlineUpdate.json
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);

    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void required_new(String bookName) {
        //http://124.70.98.107:8090/riskcensus/app/onlineUpdate.json
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);

    }

    @Transactional
    public void required_exception(String bookName) throws Exception {
        //http://124.70.98.107:8090/riskcensus/app/onlineUpdate.json
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);
        throw new RuntimeException(".....");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void required_new_exception(String bookName) throws Exception {
        //http://124.70.98.107:8090/riskcensus/app/onlineUpdate.json
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);
        throw new RuntimeException(".....");
    }

    @Transactional(propagation = Propagation.NESTED)
    public void nested(String bookName) throws Exception {
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void nested_exception(String bookName) throws Exception {
        jdbcTemplate.update("insert into book(book_name) values (?)",bookName);
        throw new RuntimeException(".....");
    }
}
